import http from '@/utils/http'
import {AnyObject} from "@/types/global";


/**
 * 查询请求体
 */
export interface QueryRequest {
    name?: string;
    comment?: string;
}
/**
 * 查询响应
 */
export interface QueryResponse  {
    name?: string;
    engine?: string;
    version?: number;
    row_format?: string;
    rows?: number;
    avg_row_length?: number;
    data_length?: number;
    max_data_length?: number;
    index_length?: number;
    data_free?: number;
    auto_increment?: number;
    create_time?: string;
    update_time?: string;
    check_time?: string;
    collation?: string;
    checksum?: string;
    create_options?: string;
    comment?: string;
}


/**
 * 基础配置
 */
export interface FieldBaseConfigResp{
    tableName: string
    tableComment: string
    tablePrefix: string
    note: string
    author: string
    time: string
    copyright: string
}
/**
 * 字段配置
 */
export interface FieldConfigResp {
    table_name: string
    column_name: string
    column_default: string
    data_type: string
    column_comment: string
    is_show: boolean
    title?: string
    column_as?: string
    width?: string
    dict_code?: string
    sort?:number
}

/**
 * 编辑配置
 */
export interface FieldEditConfigListResp {
    column_name: string
    column_comment: string
    data_type: string
    column_default: string
    show_add: boolean
    show_edit: boolean
    is_required?: boolean
    form_component?:string
    dict_code?: string
    title?:string
    sort?:number
}
export interface FieldEditConfigFormResp {
    is_show: number
    open_type: string
    open_width: number
    rows_num: number
}
export interface FieldEditConfigResp {
    from:FieldEditConfigFormResp,
    list:FieldEditConfigListResp

}

/**
 * 删除配置
 */
export interface FieldDeleteConfigFormResp {
    is_show: number
    delete_type:number | Array<number>
}
export interface FieldDeleteConfigResp {
    from:FieldDeleteConfigFormResp,
}



/**
 * 搜索配置
 */
export interface FieldSearchConfigListResp {
    column: string | undefined
    column_name: string |undefined
    column_query: string |undefined
    query_type: string | undefined,
    form_component: string | undefined,
    dict_code: string | undefined,
}
export interface FieldSearchConfigResp {
    list:FieldSearchConfigListResp,
}

/**
 * 字段配置
 */
export  interface FieldConfigResponse{
    base:FieldBaseConfigResp,
    base_list:FieldConfigResp,
    edit_config:FieldEditConfigResp,
    delete_config:FieldDeleteConfigResp,
    search_config:FieldSearchConfigResp,
}
/**
 * 查询列表
 */
export function getList(params: QueryRequest)  {
    return http.get<PageRes<QueryResponse[]>>('/admin/generate/index', params)
}
/**
 * 获取字段配置
 */
export  function getColumnsConfig(name:string){
    return http.post<FieldConfigResponse>('/admin/generate/column', {name})
}

export interface GeneratorConfigResp{
    gen_base: FieldBaseConfigResp
    gen_base_list: FieldConfigResp[]
    gen_edit_form: FieldEditConfigListResp
    gen_del_form: FieldDeleteConfigFormResp
    gen_search_list: FieldSearchConfigListResp[]
}
/**
 * 保存字段配置
 */
export function saveColumnsConfig( request: GeneratorConfigResp) {
    return http.post('/admin/generate/setColumn', request)
}

export interface GeneratePreviewResp {
    path: string
    file_name: string
    content: string
}

export interface GenerateDownloadResp {
    download_name: string
    url: string
}
/** @desc 生成预览 */
export function genPreview(tableName: string) {
    return http.post<GeneratePreviewResp[]>('/admin/generate/preview',{name:tableName})
}
/** @desc 生成代码 */
export function generate(table_name: string) {
    return http.post<GenerateDownloadResp>('/admin/generate/code',{name:table_name})
}